#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Factories;
using WaveTech.GShield.Engine.Managers;
using WaveTech.GShield.Engine.Tools;
using WaveTech.GShield.Manager.Properties;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Manager
{
	public partial class ManualNewGame : Form
	{
		#region Constructor

		public ManualNewGame()
		{
			InitializeComponent();
		}

		#endregion Constructor

		#region ManualNewGame_Load

		private void ManualNewGame_Load(object sender, EventArgs e)
		{
			m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
			                                                 m_bannerImage.Height, BannerFactory.BannerStyle.WinVistaBlack,
			                                                 new Bitmap(Resources.B256x256_Games, new Size(48, 48)), "New Game",
			                                                 "Manually Add A Game into GShield");
		}

		#endregion ManualNewGame_Load

		#region ShowFileInfo

		private void ShowFileInfo(string sFilePath)
		{
			FileVersionInfo fileVersInfo =
				FileVersionInfo.GetVersionInfo(sFilePath);

			txtCompany.Text = fileVersInfo.CompanyName;
			txtFileName.Text = StringTools.GetFileName(sFilePath);
			txtProductName.Text = fileVersInfo.ProductName;
			txtInternalName.Text = fileVersInfo.InternalName;
		}

		#endregion ShowFileInfo

		#region btnPathSearch_Click

		private void btnPathSearch_Click(object sender, EventArgs e)
		{
			if (rdoSelectByGameFile.Checked)
			{
				openFileDialog1 = new OpenFileDialog();

				openFileDialog1.InitialDirectory = "c:\\";
				openFileDialog1.Filter = "Executable Files (*.exe)|*.exe";
				openFileDialog1.Title = "Select Game Executable";
				openFileDialog1.RestoreDirectory = true;

				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					txtGamePath.Text = openFileDialog1.FileName;
					ShowFileInfo(txtGamePath.Text);
				}
			}
			else
			{
				folderBrowserDialog1 = new FolderBrowserDialog();

				if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
				{
					txtGamePath.Text = folderBrowserDialog1.SelectedPath;
				}
			}
		}

		#endregion btnPathSearch_Click

		#region btnSave_Click

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (cboRating.SelectedItem == null)
			{
				MessageBox.Show("You must select a rating.", "Add Game", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			else
			{
				if (rdoSelectByGameFile.Checked)
				{
					if (chkManualOnly.Checked)
					{
						if (
							MessageBox.Show(
								"Are you sure you want to assign the rating " + cboRating.SelectedItem + " to the game " + txtInternalName.Text +
								".", "Add New Game", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
						{
							Cursor.Current = Cursors.WaitCursor;

							Game g = GamesDAO.GetGameByPath(txtGamePath.Text);

							if (g != null)
							{
								Cursor.Current = Cursors.Default;

								MessageBox.Show(
									"The game you are trying to add, (" + txtGamePath.Text + ") already exists.", "Add New Game",
									MessageBoxButtons.OK, MessageBoxIcon.Error);
								return;
							}

							g = new Game();

							g.NotAGame = false;
							g.WasChecked = false;
							g.Company = txtCompany.Text;
							g.ProductName = txtProductName.Text;
							g.InternalName = txtInternalName.Text;
							g.FileName = txtFileName.Text;
							g.Path = txtGamePath.Text;
							g.DirectoryName = StringTools.GetDirectoryName(txtGamePath.Text, true);
							g.Name = txtInternalName.Text;
							g.DateAdded = DateTime.Now;

							if (cboRating.SelectedItem.ToString() == "Early Childhood (EC)")
							{
								g.Rating = "EC";
								g.RatingType = (int) RatingTypes.EarlyChildhood;
							}
							else if (cboRating.SelectedItem.ToString() == "Everyone (E)")
							{
								g.Rating = "E";
								g.RatingType = (int) RatingTypes.Everyone;
							}
							else if (cboRating.SelectedItem.ToString() == "Everyone 10+ (E10+)")
							{
								g.Rating = "10+";
								g.RatingType = (int) RatingTypes.Everyone10Plus;
							}
							else if (cboRating.SelectedItem.ToString() == "Teen (T)")
							{
								g.Rating = "T";
								g.RatingType = (int) RatingTypes.Teen;
							}
							else if (cboRating.SelectedItem.ToString() == "Mature (M)")
							{
								g.Rating = "M";
								g.RatingType = (int) RatingTypes.Mature;
							}
							else if (cboRating.SelectedItem.ToString() == "Adults Only (AO)")
							{
								g.Rating = "AO";
								g.RatingType = (int) RatingTypes.AdultsOnly;
							}

							GamesDAO.Save(g);

							Cursor.Current = Cursors.Default;

							MessageBox.Show("Game " + g.InternalName + " successfully added.", "Create Game", MessageBoxButtons.OK,
							                MessageBoxIcon.Information);

							Close();
						}
					}
					else
					{
						if (
							MessageBox.Show(
								"Are you sure you want to add this game automatically but with a fallback rating of " + cboRating.SelectedItem +
								"?", "Add New Game", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
						{
							Cursor.Current = Cursors.WaitCursor;

							Game g = GamesDAO.GetGameByPath(txtGamePath.Text);

							if (g != null)
							{
								Cursor.Current = Cursors.Default;

								MessageBox.Show(
									"The game you are trying to add, (" + txtGamePath.Text + ") already exists.", "Add New Game",
									MessageBoxButtons.OK, MessageBoxIcon.Error);
								return;
							}

							g =
								GameManager.Instance.GetGame(null, txtGamePath.Text, txtInternalName.Text, txtProductName.Text,
								                             txtCompany.Text, txtFileName.Text);

							if (g == null) // Auto didn't work, do it manually.
							{
								g = new Game();

								g.NotAGame = false;
								g.WasChecked = false;
								g.Company = txtCompany.Text;
								g.ProductName = txtProductName.Text;
								g.InternalName = txtInternalName.Text;
								g.FileName = txtFileName.Text;
								g.Path = txtGamePath.Text;
								g.DirectoryName = StringTools.GetDirectoryName(txtGamePath.Text, true);
								g.Name = txtFileName.Text;
								g.DateAdded = DateTime.Now;

								if (cboRating.SelectedItem.ToString() == "Early Childhood (EC)")
								{
									g.Rating = "EC";
									g.RatingType = (int) RatingTypes.EarlyChildhood;
								}
								else if (cboRating.SelectedItem.ToString() == "Everyone (E)")
								{
									g.Rating = "E";
									g.RatingType = (int) RatingTypes.Everyone;
								}
								else if (cboRating.SelectedItem.ToString() == "Everyone 10+ (E10+)")
								{
									g.Rating = "10+";
									g.RatingType = (int) RatingTypes.Everyone10Plus;
								}
								else if (cboRating.SelectedItem.ToString() == "Teen (T)")
								{
									g.Rating = "T";
									g.RatingType = (int) RatingTypes.Teen;
								}
								else if (cboRating.SelectedItem.ToString() == "Mature (M)")
								{
									g.Rating = "M";
									g.RatingType = (int) RatingTypes.Mature;
								}
								else if (cboRating.SelectedItem.ToString() == "Adults Only (AO)")
								{
									g.Rating = "AO";
									g.RatingType = (int) RatingTypes.AdultsOnly;
								}

								GamesDAO.Save(g);

								Cursor.Current = Cursors.Default;
							}

							Cursor.Current = Cursors.Default;
							Close();
						}
					}
				}
				else
				{
					// Directory Path Game
					if (
						MessageBox.Show(
							"Are you sure you want to assign the rating " + cboRating.SelectedItem + " to the game located in \n" +
							txtGamePath.Text +
							".", "Add New Game", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
					{
						Cursor.Current = Cursors.WaitCursor;

						Game g = GamesDAO.GetGameByPath(txtGamePath.Text);

						if (g != null)
						{
							Cursor.Current = Cursors.Default;

							MessageBox.Show(
								"The game directory you are trying to add, (" + txtGamePath.Text + ") already exists.", "Add New Game",
								MessageBoxButtons.OK, MessageBoxIcon.Error);
							return;
						}

						g = new Game();

						g.NotAGame = true; // It's a folder.
						g.WasChecked = false;
						g.Company = null;
						g.ProductName = null;
						g.InternalName = null;
						g.FileName = null;
						g.Path = txtGamePath.Text;
						g.DirectoryName = StringTools.GetDirectoryName(txtGamePath.Text, true);
						g.Name = txtGamePath.Text;
						g.DateAdded = DateTime.Now;

						if (cboRating.SelectedItem.ToString() == "Early Childhood (EC)")
						{
							g.Rating = "EC";
							g.RatingType = (int) RatingTypes.EarlyChildhood;
						}
						else if (cboRating.SelectedItem.ToString() == "Everyone (E)")
						{
							g.Rating = "E";
							g.RatingType = (int) RatingTypes.Everyone;
						}
						else if (cboRating.SelectedItem.ToString() == "Everyone 10+ (E10+)")
						{
							g.Rating = "10+";
							g.RatingType = (int) RatingTypes.Everyone10Plus;
						}
						else if (cboRating.SelectedItem.ToString() == "Teen (T)")
						{
							g.Rating = "T";
							g.RatingType = (int) RatingTypes.Teen;
						}
						else if (cboRating.SelectedItem.ToString() == "Mature (M)")
						{
							g.Rating = "M";
							g.RatingType = (int) RatingTypes.Mature;
						}
						else if (cboRating.SelectedItem.ToString() == "Adults Only (AO)")
						{
							g.Rating = "AO";
							g.RatingType = (int) RatingTypes.AdultsOnly;
						}

						GamesDAO.Save(g);

						Cursor.Current = Cursors.Default;

						MessageBox.Show("Game successfully added.", "Create Game", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);

						Close();
					}
				}
			}
		}

		#endregion btnSave_Click

		#region btnHelp_Click

		private void btnHelp_Click(object sender, EventArgs e)
		{
			Help.ShowHelp(this, Settings.Default.HelpFileName, HelpNavigator.TopicId,
			              Settings.Default.HelpTopics_ManuallyAddGameForm);
		}

		#endregion btnHelp_Click

		#region btnCancel_Click

		private void btnCancel_Click(object sender, EventArgs e)
		{
			Close();
		}

		#endregion btnCancel_Click

		#region rdoSelectByGameFile_CheckedChanged

		private void rdoSelectByGameFile_CheckedChanged(object sender, EventArgs e)
		{
			if (rdoSelectByGameFile.Checked)
			{
				chkManualOnly.Enabled = true;
			}
			else
			{
				chkManualOnly.Checked = false;
				chkManualOnly.Enabled = false;
			}
		}

		#endregion rdoSelectByGameFile_CheckedChanged

		#region rdoSelectByGameDirectory_CheckedChanged

		private void rdoSelectByGameDirectory_CheckedChanged(object sender, EventArgs e)
		{
			if (rdoSelectByGameDirectory.Checked)
			{
				chkManualOnly.Checked = false;
				chkManualOnly.Enabled = false;
			}
			else
			{
				chkManualOnly.Enabled = true;
			}
		}

		#endregion rdoSelectByGameDirectory_CheckedChanged
	}
}